package loan.calc.util;

import loan.calc.constant.dict.base.BaseDictEnum;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;
import java.util.stream.Stream;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DictEnumUtils {

    public static <T extends BaseDictEnum> T select(Class<T> dictEnumClazz, String value) {
        if (Objects.isNull(dictEnumClazz)) {
            return null;
        }
        return Stream.of(dictEnumClazz.getEnumConstants())
                .filter(x -> x.getValue().equalsIgnoreCase(value))
                .findAny()
                .orElse(null);
    }

    public static <T extends BaseDictEnum> T selectByDescription(Class<T> dictEnumClazz, String description) {
        return Stream.of(dictEnumClazz.getEnumConstants())
                .filter(x -> x.getDescription().equalsIgnoreCase(description))
                .findAny()
                .orElse(null);
    }

}
